﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EmptoolWebApp.Controllers
{
    public class EmployeeController : BaseController
    {
        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase[] files)
        {
            if (Request.Files.Count > 0)
            {
                //var file = Request.Files[0];

                //if (file != null && file.ContentLength > 0)
                //{
                //    var fileName = Path.GetFileName(file.FileName);
                //    var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                //    file.SaveAs(path);
                //}
            }

            return RedirectToAction("UploadDocument");
        }

        // GET: Company/About
        public ActionResult About()
        {
            return View();
        }

        // GET: Company/List
        public ActionResult List(string id)
        {
            //EmptoolWebApp.Models.CompanyListViewModel vm = new EmptoolWebApp.Models.CompanyListViewModel();

            //Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
            //    Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);

            //Microsoft.WindowsAzure.Storage.Table.TableQuery<EmptoolWebApp.Entities.CompanyEntity> query =
            //    new Microsoft.WindowsAzure.Storage.Table.TableQuery<Entities.CompanyEntity>();
            //List<EmptoolWebApp.Entities.CompanyEntity> list = tbl.ExecuteQuery<EmptoolWebApp.Entities.CompanyEntity>(query).ToList();

            //vm.CompanyEntityList = list;

            Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.EmployeeEntity.TABLE_NAME);
            Microsoft.WindowsAzure.Storage.Table.TableQuery<EmptoolWebApp.Entities.EmployeeEntity> query =
                new Microsoft.WindowsAzure.Storage.Table.TableQuery<Entities.EmployeeEntity>();
            List<EmptoolWebApp.Entities.EmployeeEntity> list = tbl.ExecuteQuery<EmptoolWebApp.Entities.EmployeeEntity>(query).ToList();

            // Fill ViewModel
            EmptoolWebApp.Models.EmployeeListViewModel vm =
                new EmptoolWebApp.Models.EmployeeListViewModel();
            vm.CompanyPK = id;
            vm.CompanyName = Helpers.CompanyHelper.GetCompanyName(id);
            vm.EmployeeEntityList = list;

            return View(vm);
        }

        // GET: Employee
        public ActionResult Index()
        {
            EmptoolWebApp.Models.EmployeeListViewModel vm =
                new EmptoolWebApp.Models.EmployeeListViewModel();
            return View(vm);
        }

        // GET: Employee/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Employee/Create
        public ActionResult Create(string id)
        {
            Models.EmployeeViewModel vm = new Models.EmployeeViewModel();
            vm.CompanyPK = id;
            vm.CompanyName = Helpers.CompanyHelper.GetCompanyName(id);

            return View(vm);
        }

        // POST: Employee/Create
        [HttpPost]
        public ActionResult Create(Models.EmployeeViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Copy model data to table storage entity
                    EmptoolWebApp.Entities.EmployeeEntity employee = new Entities.EmployeeEntity(model.CompanyName, User.Identity.Name);
                    employee.Owner = User.Identity.Name;
                    employee.Creator = User.Identity.Name;
                    employee.FirstName = model.FirstName;
                    employee.LastName = model.LastName;
                    employee.Department= model.Department;
                    employee.JobTitle = model.JobTitle;
                    

                    EmptoolWebApp.Entities.CompanyAssociateEntity companyAssociate = new Entities.CompanyAssociateEntity(
                        model.CompanyName, User.Identity.Name, Entities.CompanyAssociationType.EMPLOYEE);
                    companyAssociate.Owner = User.Identity.Name;
                    companyAssociate.Creator = User.Identity.Name;

                    // Step-by-step syntax
                    Microsoft.WindowsAzure.Storage.Table.TableOperation insertOperation =
                        Microsoft.WindowsAzure.Storage.Table.TableOperation.Insert(employee);
                    Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                        Helpers.AzureStorageHelper.GetStorageTable(Entities.EmployeeEntity.TABLE_NAME);
                    tbl.Execute(insertOperation);

                    insertOperation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Insert(companyAssociate);
                    tbl = Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyAssociateEntity.TABLE_NAME);
                    tbl.Execute(insertOperation);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Employee/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Employee/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Employee/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Employee/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
